package com.example.pokemon_chat_room;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.pojo.Pokemon;
import com.example.pokemon_chat_room.util.MyImageView;

import org.w3c.dom.Text;

public class PokemonDetail extends AppCompatActivity {

    private MyImageView pokemon_img;

    private TextView name_tv;

    private TextView number_tv;

    private LinearLayout attribute_layout;

    private TextView classification_tv;

    private TextView characteristic_tv;

    private TextView rarity_tv;

    private TextView rate_tv;

    private TextView introduce_tv;

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String menuTitle = "宝可梦详情";
        androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.title_layout);//设置标题样式
            TextView textView = (TextView) actionBar.getCustomView().findViewById(R.id.display_title);//获取标题布局的textview
            textView.setText(menuTitle);//设置标题名称，menuTitle为String字符串
            actionBar.setHomeButtonEnabled(true);//设置左上角的图标是否可以点击
            actionBar.setDisplayHomeAsUpEnabled(true);//给左上角图标的左边加上一个返回的图标
            actionBar.setDisplayShowCustomEnabled(true);// 使自定义的普通View能在title栏显示，即actionBar.setCustomView能起作用

        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pokemon_detail);

        pokemon_img = findViewById(R.id.pokemon_img);
        name_tv = findViewById(R.id.name_tv);
        number_tv = findViewById(R.id.number_tv);
        attribute_layout = findViewById(R.id.attribute_layout);
        classification_tv = findViewById(R.id.classification_tv);
        characteristic_tv = findViewById(R.id.characteristic_tv);
        rarity_tv = findViewById(R.id.rarity_tv);
        rate_tv = findViewById(R.id.rate_tv);
        introduce_tv = findViewById(R.id.introduce_tv);


        Intent intent = getIntent();

        String data = intent.getStringExtra("pokemon");
        JSONObject jsonObject = JSONObject.parseObject(data);
        Pokemon pokemon = jsonObject.toJavaObject(Pokemon.class);
        pokemon_img.setImageURL("poke/" + pokemon.getImage());
        name_tv.setText(pokemon.getName());
        String number = pokemon.getId().toString();
        if (number.length() == 1) {
            number = "No.00" + number;
        } else {
            number = "No.0" + number;
        }
        number_tv.setText(number);
        String[] att = pokemon.getAttribute().split(",");
        for (int i = 0; i < att.length; i++) {
            MyImageView myImageView = new MyImageView(this);
            myImageView.setLayoutParams(new LinearLayout.LayoutParams(79*3, 23*3 ));

            myImageView.setImageURL("poke/att"+att[i]+".png");
            myImageView.setPadding(10, 0, 0, 0);
            attribute_layout.addView(myImageView);
        }
        classification_tv.setText(pokemon.getClassification());
        characteristic_tv.setText(pokemon.getCharacteristic());
        rarity_tv.setText(getRarity(pokemon.getRarity()));
        rate_tv.setText(pokemon.getRate() + "%");
        introduce_tv.setText(pokemon.getIntroduce());
    }

    private String getRarity(Integer num) {
        switch (num) {
            case 1:
                return "超常见";
            case 2:
                return "常见";
            case 3:
                return "普通";
            case 4:
                return "稀有";
            case 5:
                return "超稀有";
            case 6:
                return "超超稀有";
            case 7:
                return "极其稀有";
            default:
                return "未记录";
        }
    }
}